package com.aotuman.framework;

import android.os.Bundle;

import com.aotuman.framework.core.BaseActivity;
import com.aotuman.framework.core.ITabFragment;
import com.aotuman.framework.sample.SampleListFragment;
import com.aotuman.framework.sample.SampleSectionListFragment;
import com.aotuman.framework.widgets.tab.TabLayout;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements TabLayout.OnTabClickListener{
    private TabLayout mTabLayout;
    private ITabFragment fragment;

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_sample_tab, R.string.title_framework_main, MODE_HOME);
    }

    @Override
    protected void setUpView() {
        mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        ArrayList<TabLayout.Tab> tabs = new ArrayList<>();
        tabs.add(new TabLayout.Tab(R.drawable.selector_tab_msg, R.string.home_tab_news, R.menu.menu_home, SampleListFragment.class));
        tabs.add(new TabLayout.Tab(R.drawable.selector_tab_moments, R.string.home_tab_topic, R.menu.menu_home, SampleSectionListFragment.class));
        mTabLayout.setUpData(tabs, this);
        mTabLayout.setCurrentTab(0);
    }

    @Override
    public void onTabClick(TabLayout.Tab tab) {
        try {
            setUpTitle(tab.labelResId);
            setUpMenu(tab.menuResId);


            ITabFragment tmpFragment = (ITabFragment) getSupportFragmentManager().findFragmentByTag(tab.targetFragmentClz.getSimpleName());
            if (tmpFragment == null) {
                tmpFragment = tab.targetFragmentClz.newInstance();
                if (fragment == null) {
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.mFragmentContainerLayout, tmpFragment.getFragment(), tab.targetFragmentClz.getSimpleName())
                            .commit();
                } else {
                    getSupportFragmentManager().beginTransaction()
                            .hide(fragment.getFragment())
                            .add(R.id.mFragmentContainerLayout, tmpFragment.getFragment(), tab.targetFragmentClz.getSimpleName())
                            .commit();
                }
            } else {
                getSupportFragmentManager().beginTransaction()
                        .hide(fragment.getFragment())
                        .show(tmpFragment.getFragment())
                        .commit();
            }
            fragment = tmpFragment;

//            fragment = tab.targetFragmentClz.newInstance();
//            getSupportFragmentManager().beginTransaction().
//                    replace(R.id.mFragmentContainerLayout, fragment.getFragment())
//                    .commitAllowingStateLoss();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
